package com.jsyl.lwbk.business.service.impl;

import com.jsyl.lwbk.business.domain.Integration;
import com.jsyl.lwbk.business.domain.TrainPackageProject;
import com.jsyl.lwbk.business.dto.TrainPackageProjectDTO;
import com.jsyl.lwbk.business.mapper.ChildProjectConfigMapper;
import com.jsyl.lwbk.business.mapper.IntegrationMapper;
import com.jsyl.lwbk.business.mapper.education.CourseChildMapper;
import com.jsyl.lwbk.business.service.TrainPackageProjectService;
import com.jsyl.lwbk.business.domain.ChildProjectConfig;
import com.jsyl.lwbk.business.domain.education.ChildCourse;
import com.jsyl.lwbk.business.dto.SaveProjectDTO;
import com.jsyl.lwbk.business.mapper.TrainPackageProjectMapper;
import com.jsyl.lwbk.business.mapper.education.ChildCourseMapper;
import com.jsyl.lwbk.common.core.domain.Ztree;
import com.jsyl.lwbk.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Fitch
 * 2022/2/21
 */
@Service
public class TrainPackageProjectServiceImpl implements TrainPackageProjectService {
    @Autowired
    private TrainPackageProjectMapper trainPackageProjectMapper;
    @Autowired
    private IntegrationMapper integrationMapper;
    @Autowired
    private ChildProjectConfigMapper childProjectConfigMapper;
    @Autowired
    private CourseChildMapper courseChildMapper;
    @Autowired
    private ChildCourseMapper childCourseMapper;


    @Override
    public List<TrainPackageProjectDTO> findList(Long trainPackageId) {
        List<TrainPackageProjectDTO> list = this.trainPackageProjectMapper.findList(trainPackageId);
        Map<String, String> codeName =getCodeName();
        for(TrainPackageProjectDTO c:list){
            if(c.getProjectCode() !=null){
                List<String> projectCodeList=new ArrayList<>();
                //项目编号转换名称
                String name  = c.getProjectCode();
                String[] projectCodes= c.getProjectCode().split("\\|");
                for(String s:projectCodes){
                    String[] projectCodes1= s.split(",");
                    for(String s1:projectCodes1){
                        projectCodeList.add(s1);
                        name= name.replace(s1,codeName.get(s1));
                    }
                }
                c.setProjectName(name);
            }

        }
        return list;
    }
    @Override
    public List<TrainPackageProjectDTO> listByCourse(Long trainPackageId,Long childCourseId) {
        ChildCourse childCourse =this.childCourseMapper.selectById(childCourseId);
        List<TrainPackageProjectDTO> list = this.trainPackageProjectMapper.findListByClass(trainPackageId,childCourse.getClassId());
        Map<String, String> codeName =getCodeName();
        for(TrainPackageProjectDTO c:list){
            if(c.getProjectCode() !=null){
                List<String> projectCodeList=new ArrayList<>();
                //项目编号转换名称
                String name  = c.getProjectCode();
                String[] projectCodes= c.getProjectCode().split("\\|");
                for(String s:projectCodes){
                    String[] projectCodes1= s.split(",");
                    for(String s1:projectCodes1){
                        projectCodeList.add(s1);
                        name= name.replace(s1,codeName.get(s1));
                    }
                }
                c.setProjectName(name);
                //各个项目本班级学员训练次数统计
                List<Long> childIdList = this.courseChildMapper.listChildIds(childCourseId);
                List<ChildProjectConfig> configList = this.childProjectConfigMapper.listCompleteTimeByCourse(childCourseId,projectCodeList,childIdList);
                Map<String,String> temp =new HashMap<>();
                for(ChildProjectConfig config:configList){
                    if(config.getCompleteTimes().equals(0)){
                        continue;
                    }
                    if(temp.containsKey(config.getProjectName())){
                        temp.put(config.getProjectName(),(temp.get(config.getProjectName())+";"+config.getChildName()+":"+config.getCompleteTimes()+"次"));
                    }else {
                        temp.put(config.getProjectName(),config.getChildName()+":"+config.getCompleteTimes()+"次");
                    }
                }
                String completeInfo="";
                for(Map.Entry entry:temp.entrySet()){
                    completeInfo+="|"+entry.getKey()+":"+entry.getValue();
                }
                c.setCompleteInfo(completeInfo);

            }

        }
        return list;
    }

    private Map<String, String> getCodeName() {
        List<Integration> list = integrationMapper.findList(null);
        Map<String,String> result =new HashMap<>();
        for(Integration i:list){
            result.put(i.getCode(),i.getName());
        }
        return result;
    }

    @Override
    public int addSubject(TrainPackageProjectDTO dto) {
        TrainPackageProject trainPackageProject =new TrainPackageProject();
//        trainPackageProject.setId(IdWorker.getId());
        trainPackageProject.setTrainPackageId(Long.valueOf(dto.getTrainPackageId()));
        trainPackageProject.setSubjectName(dto.getSubjectName());
        trainPackageProject.setCreateUser(dto.getCreateUser());
        //保存项目信息
        trainPackageProject.setProjectCode(null);
        if (dto.getIntegrationIds() != null) {
            String projectCode = this.integrationMapper.getCodeById(CollectionUtils.arrayToList(dto.getIntegrationIds().split(",")));
            trainPackageProject.setProjectCode(projectCode);
        }
        return this.trainPackageProjectMapper.insert(trainPackageProject);
    }
    @Override
    public int saveProject(SaveProjectDTO dto) {
        TrainPackageProject trainPackageProject = this.trainPackageProjectMapper.getById(Long.valueOf(dto.getId()));
        if (trainPackageProject == null) {
            return 0;
        }
        //保存项目信息
        trainPackageProject.setProjectCode(null);
        if (dto.getIntegrationIds() != null) {
            String projectCode = this.integrationMapper.getCodeById(CollectionUtils.arrayToList(dto.getIntegrationIds().split(",")));
            trainPackageProject.setProjectCode(projectCode);
        }
        trainPackageProject.setUpdateUser(dto.getUpdateUser());
        return this.trainPackageProjectMapper.update(trainPackageProject);
    }
    @Override
    public List<Ztree> projectTree(Long trainPackageProjectId) {
        List<Integer> leafType = new ArrayList<>();
        leafType.add(1);
        leafType.add(2);
        leafType.add(4);
        List<Integration> integrationList = this.integrationMapper.findList(leafType);
        if (integrationList == null) {
            return new ArrayList<Ztree>();
        }
        List<String> checkedList = new ArrayList<>();
        TrainPackageProject trainPackageProject = this.trainPackageProjectMapper.getById(trainPackageProjectId);
        if (trainPackageProject != null && trainPackageProject.getProjectCode() != null) {
            checkedList = CollectionUtils.arrayToList(trainPackageProject.getProjectCode().split(","));
        }
        return initProjectTree(integrationList, checkedList, false);
    }

    public List<Ztree> initProjectTree(List<Integration> menuList, List<String> checkedList, boolean permsFlag) {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = CollectionUtils.isEmpty(checkedList);
        for (Integration menu : menuList) {
            Ztree ztree = new Ztree();
            ztree.setId(menu.getId());
            ztree.setpId(menu.getParentId());
            ztree.setName(menu.getName());
            ztree.setTitle(menu.getName());
            if (!isCheck) {
                ztree.setChecked(checkedList.contains(menu.getCode()));
            }
            ztrees.add(ztree);
        }
        return ztrees;
    }

    @Override
    public int sortProject(SaveProjectDTO dto) {
        TrainPackageProject trainPackageProject = this.trainPackageProjectMapper.getById(Long.valueOf(dto.getTrainPackageId()));
        if (trainPackageProject == null) {
            return 0;
        }
        //保存项目信息
        trainPackageProject.setProjectCode(StringUtils.list2String(dto.getProjectCodes(),","));
        trainPackageProject.setUpdateUser(dto.getUpdateUser());
        return this.trainPackageProjectMapper.update(trainPackageProject);
    }

    @Override
    public int deleteById(String id) {
        return this.trainPackageProjectMapper.deleteById(id);
    }
}
